//
//  GXPicturetableViewCell.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/28.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXPicturetableViewCell.h"
#import "GXSuggestModel.h"
#import "UIImageView+WebCache.h"
#import "NSString+GXSizeWithText.h"
#import "UIImage+GIF.h"
//#import "FontSizeManager.h"

#define kCellWidth self.contentView.frame.size.width
//图片高度
#define kMainViewHeight 300
//标题字号
#define kTitleLabelFontSize 20
//副标题字号
#define kSubTitleLabelFontSize 15
//简介字号
#define kIntroLabelFontSize 18
//边界
#define kSpecialCommentBorder 20
//九宫格图片高度
#define kImageViewHeight 130
//按钮
#define kSpecialBtnSize 40
//时间高度
#define kTimeLabelHeight 20
//作者头像边长
#define kAuthorImageViewSize 30
//控件之间的宽度
#define kStringBorder 10

@interface GXPicturetableViewCell ()

@property (nonatomic, retain) UIView *mainView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UILabel *descLabel;
@property (nonatomic, retain) UIImageView *qouteImageView;
@property (nonatomic, retain) UILabel *imageCountLabel;

@end

@implementation GXPicturetableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeModel) name:@"changeModel" object:nil];
    
    //九宫格
    self.mainView = [[UIView alloc] init];
    [self.contentView addSubview:self.mainView];
    
    //创建主要视图中的9张图片
    for (NSInteger i = 0; i < 9; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.mainView addSubview:imageView];
//        [imageView setBackgroundColor:[UIColor redColor]];
    }
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    [self.titleLabel setFont:[UIFont systemFontOfSize:kTitleLabelFontSize]];
    [self.titleLabel setNumberOfLines:0];
    //引号
    self.qouteImageView = [[UIImageView alloc] init];
    self.qouteImageView.image = [UIImage imageNamed:@"icon-quote-black"];
    [self.contentView addSubview:self.qouteImageView];

    //简介
    self.introLabel = [[UILabel alloc] init];
    [self.introLabel setFont:[UIFont systemFontOfSize:kIntroLabelFontSize]];
    [self.introLabel setNumberOfLines:0];
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.introLabel setTextAlignment:NSTextAlignmentCenter];
    [self.contentView addSubview:self.introLabel];

    //描述
    self.descLabel = [[UILabel alloc] init];
    [self.descLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    [self.descLabel setTextAlignment:NSTextAlignmentCenter];
    self.descLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.descLabel setNumberOfLines:0];
    [self.contentView addSubview:self.descLabel];

    //图片数量
    self.imageCountLabel = [[UILabel alloc]  init];
    [self.imageCountLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    [self.imageCountLabel setTextAlignment:NSTextAlignmentRight];
    [self.contentView addSubview:self.imageCountLabel];
}

- (void)changeModel {
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    self.descLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //主要视图
    [self.mainView setFrame:CGRectMake(0, kStringBorder, kCellWidth,  kImageViewHeight * 3 + 6)];
    //创建九宫格图片
    CGFloat imageViewWidth = (kCellWidth - 6) / 3;
    CGRect imageFrame = CGRectMake(0, 0, imageViewWidth, kImageViewHeight);
    for (NSInteger i = 0; i < 9; i++) {
        UIImageView *imageView = [self.mainView subviews][i];
        [imageView setFrame:imageFrame];
        imageFrame.origin.x += imageViewWidth + 3;
        if ((i+1) % 3 == 0) {
            imageFrame.origin.y += kImageViewHeight + 3;
            imageFrame.origin.x = 0;
        }
    }
    CGFloat labelW = kCellWidth - 2 * kSpecialCommentBorder;
    //标题
    CGFloat titleLabelY = CGRectGetMaxY(self.mainView.frame) + kStringBorder;
    CGRect titleLabelSize = [self.titleLabel.text SizeWithTextFontSize:kTitleLabelFontSize textWidth:labelW];
    [self.titleLabel setFrame:CGRectMake(kSpecialCommentBorder, titleLabelY, labelW, titleLabelSize.size.height)];
    //引号
    CGFloat qouteImageY = CGRectGetMaxY(self.titleLabel.frame) + kStringBorder;
    CGSize qouteImageSize = self.qouteImageView.image.size;
    [self.qouteImageView setFrame:CGRectMake(kCellWidth / 2.0 - qouteImageSize.width / 2.0, qouteImageY, qouteImageSize.width, qouteImageSize.height)];
    //简介
    CGFloat introLabelY = CGRectGetMaxY(self.qouteImageView.frame);
    CGRect introLabelSize = [self.introLabel.text SizeWithTextFontSize:kIntroLabelFontSize textWidth:labelW];
    [self.introLabel setFrame:CGRectMake(kSpecialCommentBorder, introLabelY, labelW, introLabelSize.size.height)];
    //描述
    CGFloat descLabelY = CGRectGetMaxY(self.introLabel.frame) + kStringBorder;
    CGRect descLabelSize = [self.descLabel.text SizeWithTextFontSize:kSubTitleLabelFontSize textWidth:labelW];
    [self.descLabel setFrame:CGRectMake(kSpecialCommentBorder, descLabelY, labelW, descLabelSize.size.height)];
    //图片数量
    CGFloat imageLabelY = self.contentView.frame.size.height - kSpecialBtnSize;
    [self.imageCountLabel setFrame:CGRectMake(kCellWidth - 150 - kSpecialCommentBorder, imageLabelY, 150, 40)];
}

- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;

    }
        //九宫格相册
    if (suggestModel.type.integerValue == 1) {
        
        for (NSInteger i = 0; i < 9; i++) {
                UIImageView *imageView = [self.mainView subviews][i];
                [imageView sd_setImageWithURL:_suggestModel.gallery[@"images"][i][@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
        }
    }
    //标题
    self.titleLabel.text = _suggestModel.title;
    //简介
    self.introLabel.text = _suggestModel.intro;
    //描述
    self.descLabel.text = _suggestModel.content[@"desc"];
    //图片数量
    self.imageCountLabel.text = [NSString stringWithFormat:@"%@图", _suggestModel.gallery[@"image_count"]];
}

@end
